package com.mindskip.xzs.controller.student;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.Video;
import com.mindskip.xzs.domain.VideoUser;
import com.mindskip.xzs.service.VideoService;
import com.mindskip.xzs.service.VideoUserService;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.education.VideoPageRequestVM;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author changjiang
 * @Title:
 * @Package
 * @Description: 视频添加
 * @date 2022/1/263:13 下午
 */
@RestController("StudentVideoController")
@RequestMapping(value = "/api/student/video")
public class VideoController extends BaseApiController {

    private final VideoService videoService;

    private final VideoUserService videoUserService;

    public static final String key = "1";

    public VideoController(VideoService videoService, VideoUserService videoUserService) {
        this.videoService = videoService;
        this.videoUserService = videoUserService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public RestResponse<List<Video>> pageList(@RequestBody VideoPageRequestVM model) {
        String videoName = model.getVideoName();
        if (StringUtils.isEmpty(videoName) || "".equals(videoName)) {
            model.setVideoName(null);
        }
        List<Video> pageInfo = videoService.videoPage(model);
        String userId = model.getUserId();
        for (Video video : pageInfo) {
            VideoUser videoUser = new VideoUser();
            videoUser.setUserUuid(userId);
            videoUser.setVideoId(video.getId().toString());
            VideoUser videoUser1 = videoUserService.getForUserIdAndVideoId(videoUser);
            if (null != videoUser1) {
                video.setVideoRate(videoUser1.getVideoRate());
                video.setVideoWatch(videoUser1.getVideoWatch());
            } else {
                video.setVideoRate("0");
            }
        }
        return RestResponse.ok(pageInfo);
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<Video>> pageList2(@RequestBody VideoPageRequestVM model) {
        String videoName = model.getVideoName();
        if (StringUtils.isEmpty(videoName) || "".equals(videoName)) {
            model.setVideoName(null);
        }
        if (null == model.getVideoType()) {
            model.setVideoType(0);
        }
        PageInfo<Video> pageInfo = videoService.page(model);
        String userId = model.getUserId();
        if (StringUtils.isEmpty(userId)) {
            return RestResponse.fail(10001, "用户id不能为空");
        }
        for (Video video : pageInfo.getList()) {
            VideoUser videoUser = new VideoUser();
            videoUser.setUserUuid(userId);
            videoUser.setVideoId(video.getId().toString());
            VideoUser videoUser1 = videoUserService.getForUserIdAndVideoId(videoUser);
            if (null != videoUser1) {
                video.setVideoRate(videoUser1.getVideoRate());
                video.setVideoWatch(videoUser1.getVideoWatch());
            } else {
                video.setVideoRate("0");
            }
        }
        return RestResponse.ok(pageInfo);
    }


    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid Video model) {
        int vm = videoService.updateById(model);
        return RestResponse.ok(vm);
    }


    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<Video> select(@PathVariable Integer id) {
        Video vm = videoService.selectById(id);
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        Video video = videoService.selectById(id);
        video.setState("1");
        videoService.updateByIdFilter(video);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public RestResponse deleteById(@RequestBody @Valid Video model) {
        model.setDeleted(1);
        videoService.updateByIdFilter(model);
        return RestResponse.ok();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RestResponse add(@RequestBody @Valid Video model) {
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        //model.setTag(getCurrentUser().getRealName());
        int vm = 0;
        if (null == model.getId()) {
            vm = videoService.insertSelective(model);
        } else {
            vm = videoService.updateByIdFilter(model);
        }
        return RestResponse.ok(vm);
    }

    @RequestMapping(value = "/watch/rate", method = RequestMethod.POST)
    public RestResponse watchRate(@RequestBody @Valid VideoUser videoUser) {
        String videoId = videoUser.getVideoId();
        String userUuid = videoUser.getUserUuid();
        if (StringUtils.isEmpty(videoId) || StringUtils.isEmpty(userUuid)) {
            return RestResponse.fail(10001, "用户id或视频id不能为空");
        }
        String videoTime = videoUser.getVideoTime();
        String videoWatch = videoUser.getVideoWatch();
        if (StringUtils.isEmpty(videoTime)) {
            return RestResponse.fail(10001, "观看时间过短");
        }
        java.text.NumberFormat numberFormat = java.text.NumberFormat.getInstance();
        //设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        Float videoTotal = Float.parseFloat(videoTime);
        Float videoNow = Float.parseFloat(videoWatch);
        String rate1 = numberFormat.format((videoNow / videoTotal) * 100);
        if (StringUtils.isEmpty(rate1) || "0".equals(rate1) || videoTotal.equals(0.00)) {
            rate1 = "1";
        }
        if ("0".equals(videoTime)) {
            rate1 = "0";
        }
        String rate = rate1 + "%";
        videoUser.setVideoRate(rate);
        //根据视频id和用户id查询该条是否存在，存在更新
        synchronized (key) {
            VideoUser videoUser1 = videoUserService.getForUserIdAndVideoId(videoUser);
            if (null == videoUser1) {
                videoUser.setCreateTime(new Date());
                videoUser.setUpdateTime(new Date());
                videoUserService.insertSelective(videoUser);
            } else {
                videoUser.setUpdateTime(new Date());
                videoUserService.updateByPrimaryKeySelective(videoUser);
            }
        }
        return RestResponse.ok();
    }

}
